#ifndef COMMON_CACHED_TRANSFORM_H
#define COMMON_CACHED_TRANSFORM_H

#include <unordered_map>
#include <functional>

template<typename T, typename U>
class CachedTransform
{
private:
    std::function<U(const T&)> transform;
    std::unordered_map<T, U> cache;
public:
    explicit CachedTransform(const std::function<U(const T&)>& transform_) : transform(transform_) { }
    U GetOrCreateCache(const T& in)
    {
        auto iter = cache.find(in);
        if (iter != cache.end())
            return iter->second;
        auto out = transform(in);
        cache.insert(std::make_pair(in, out));
        return out;
    }
};

#endif //COMMON_CACHED_TRANSFORM_H
